package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    /**
     * 分页查询用户声音列表
     * @param trackListVoPage
     * @param trackInfoQuery
     * @return
     */
    IPage<TrackListVo> selectUserTrackPage(Page<TrackListVo> trackListVoPage, @Param("vo") TrackInfoQuery trackInfoQuery);

    /**
     * 修改声音的排序
     * @param orderNum
     * @param albumId
     */
    @Update("update track_info set order_num = order_num - 1 where order_num > #{orderNum} and album_id = #{albumId} and is_deleted = 0")
    void updateOrderNum(@Param("orderNum") Integer orderNum, @Param("albumId") Long albumId);

    /**
     * 分页查询专辑下的声音列表
     * @param albumTrackListVoPage
     * @param albumId
     * @return
     */
    @Select("select\n" +
            "    track.id as trackId,\n" +
            "    track.track_title,\n" +
            "    track.media_duration,\n" +
            "    track.order_num,\n" +
            "    track.create_time                                    as createTime,\n" +
            "    MAX(IF(stat.stat_type = '0701', stat.stat_num, 0))   as playStatNum,\n" +
            "    MAX(IF(stat.stat_type = '0704', stat.stat_num, 0))   as commentStatNum\n" +
            "from track_info track\n" +
            "         inner join track_stat stat on stat.track_id = track.id\n" +
            "where track.album_id = #{albumId}\n" +
            "  and track.status = '0501'\n" +
            "  and track.is_open = '1'\n" +
            "  and track.is_deleted = 0\n" +
            "group by track.id")
    IPage<AlbumTrackListVo> selectAlbumTrackPage(Page<AlbumTrackListVo> albumTrackListVoPage, @Param("albumId") Long albumId);
}
